import React, { Component, ComponentClass } from 'react';
import {View} from '@tarojs/components'
import {GetRequest,vcmonitor} from "../../utils/tools";
import {newHttp} from "../../utils/request";
import { PC_REPAY} from "../../constants/api";
import {payment} from "../../utils/payment.service";

type PageStateProps = {}

type PageDispatchProps = {
  getSet: any
}

type PageOwnProps = {}

type PageState = {}

type IProps = PageStateProps & PageDispatchProps & PageOwnProps

interface Login {
  props: IProps
}


class PayRoot extends Component {
  state: {
    infoData: any
    uuid: string
    payToken: string
    touuid: string
    productName: string
    sName: string
    type: string
    amount: number
    orderNum: number
  } = {
    uuid: '',
    payToken: '',
    infoData: {
      amount: 0,
      sName: '微程之家',
      productName: '商品信息'
    },
    productName:'',
    sName:'',
    touuid:'',
    amount:0,
    type:'',
    orderNum: ''
  }

  async componentDidMount() {
    // const {type='',payToken='',uuid='',touuid='',amount,productName,sName ,orderNum} =  Taro.getCurrentInstance().router.params
    // this.setState({orderNum})

  }
  async componentDidShow() {
    vcmonitor(this)
    const {order_num} =  Taro.getCurrentInstance().router.params
    this.setState({orderNum:order_num},()=>{
      this.goPay()
    })
  }

  async goPay() {
    const {orderNum} = this.state
    const result = await newHttp.put(PC_REPAY + orderNum)
    if(result){
    payment.payment(result.data.data, false, res => {
      if (process.env.TARO_ENV === 'h5') {
        document.addEventListener('WeixinJSBridgeReady', function () {
          WeixinJSBridge.call('closeWindow');
        }, false);
        WeixinJSBridge.call('closeWindow');
      }
    })
    }
  }

  render() {
    const {infoData, uuid,touuid,productName,amount,sName} = this.state
    return (
      <View className='pay-root'>
        {/*{touuid ?*/}
        {/*  <Block>*/}
        {/*    <View className='goods-name'>{productName}</View>*/}
        {/*    <View className='goods-price'>{amount}</View>*/}
        {/*    <View className='price-name'>*/}
        {/*      <View className='price-name-left'>收款方</View>*/}
        {/*      <View className='price-name-right'>{sName}</View>*/}
        {/*    </View>*/}
        {/*    <View className='pay-btn' onClick={this.goRecharge.bind(this)}>支付</View>*/}
        {/*  </Block>*/}
        {/*  :*/}
        {/*  <Block>*/}
        {/*    <View className='goods-name'>{infoData.productName}</View>*/}
        {/*    <View className='goods-price'>{infoData.amount}</View>*/}
        {/*    <View className='price-name'>*/}
        {/*      <View className='price-name-left'>收款方</View>*/}
        {/*      <View className='price-name-right'>{infoData.sName}</View>*/}
        {/*    </View>*/}
        {/*    <View className='pay-btn' onClick={this.goPay.bind(this)}>支付</View>*/}
        {/*  </Block>*/}
        {/*}*/}
      </View>
    )
  }
}

export default PayRoot as ComponentClass<PageOwnProps, PageState>
